package cn.stronglink.assetmanage.business.inventory.view.count;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import cn.stronglink.assetmanage.R;
import cn.stronglink.assetmanage.business.inventory.adapter.InventoryListAdapter;
import cn.stronglink.assetmanage.business.inventory.adapter.InventoryOrderAdapter;
import cn.stronglink.assetmanage.business.inventory.model.InventoryCountModel;
import cn.stronglink.assetmanage.business.inventory.presenter.InventoryCountPresenter;
import cn.stronglink.assetmanage.business.inventory.view.IRefreshList;
import cn.stronglink.assetmanage.entity.Inventory;
import cn.stronglink.assetmanage.index.view.onRecyclerListener;
import cn.stronglink.assetmanage.utils.Constants;
import cn.stronglink.assetmanage.widget.ViewPagerFragment;

/**
 * 已经盘点列表
 */
public class InventoryCountFragment extends ViewPagerFragment implements IRefreshList {
    private RecyclerView rv_inventorycount_order_list;
    private ExpandableListView rv_inventorycount_list;
    private InventoryListAdapter listAdapter;
    private InventoryOrderAdapter orderAdapter;
    private InventoryCountModel inventoryCountModel;
    private InventoryCountView inventoryCountView;
    private InventoryCountPresenter inventoryCountPresenter;
    private List<String> groupNameList;
    private LinearLayoutManager layoutmanager;
    private TextView  txt_main_title;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_inventory_count, container, false);
        try {
            initViewAndData(rootView);
        } catch (Exception e) {
            Toast.makeText(this.getContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
        }
        return rootView;
    }

    private void initViewAndData(View view) {
        txt_main_title = this.getActivity().findViewById(R.id.txt_main_title);
        txt_main_title.setText("盘点管理");
        rv_inventorycount_order_list = view.findViewById(R.id.rv_inventorycount_order_list);
        rv_inventorycount_list = view.findViewById(R.id.rv_inventorycount_list);
        layoutmanager = new LinearLayoutManager(this.getActivity(), LinearLayoutManager.VERTICAL, false);
        rv_inventorycount_order_list.setLayoutManager(layoutmanager);
        listAdapter = new InventoryListAdapter(getActivity(), null);
        groupNameList = new ArrayList<>();
        groupNameList.add(getResources().getString(R.string.inventory_more));
        groupNameList.add(getResources().getString(R.string.inventory_normal));
        orderAdapter = new InventoryOrderAdapter(getActivity(), groupNameList);
        rv_inventorycount_order_list.setAdapter(listAdapter);
        rv_inventorycount_list.setAdapter(orderAdapter);
        listAdapter.setOnItemClickListener(new onRecyclerListener() {
            @Override
            public void onItemClicked(View v, int postion) {
                inventoryCountPresenter.showOrderByPosition(postion);
            }
        });
        rv_inventorycount_list.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                String groupName = groupNameList.get(groupPosition);
                inventoryCountPresenter.showDetailByPosition(groupName, childPosition);
                return false;
            }
        });
        inventoryCountModel = new InventoryCountModel();
        inventoryCountView = new InventoryCountView(this, view, listAdapter, orderAdapter);
        inventoryCountPresenter = new InventoryCountPresenter(inventoryCountModel, inventoryCountView);
    }

    @Override
    protected void onFragmentVisibleChange(boolean isVisible) {
        try {
            if(isVisible) {
                this.refreshList();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void refreshList() {
        refreshListHandler.sendEmptyMessage(0);
    }

    private Handler refreshListHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            refreshData();
        }

        private void refreshData() {
            try {
                inventoryCountPresenter.initInventoryListView();
            } catch (Exception e) {
                Toast.makeText(InventoryCountFragment.this.getContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
            }
        }
    };
}
